﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using VT_Tool_DLL;

namespace Module_Debug.Visionox_Gamma
{
    public partial class VT5100_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        private static bool Gamma_adjust_timeout_flag = false;

        public int[] Parttern_VTDR6100_1D = new int[25] { 255, 223, 191, 159, 127, 111, 95, 79, 71, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 5, 3, 1, 0 };
        public int[] Parttern_VTDR6100 = new int[14] { 255, 223, 191, 127, 111, 79, 63, 55, 47, 39, 35, 31, 11, 3 };

        frmMain frmHelp = null;
        public VT5100_Gamma(frmMain VT5100)
        {
            frmHelp = VT5100;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体            

        }
        //VT5100 监听卸载
        private void VT5100_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(VTDR6100_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "VT5100_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void VT5100_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(VTDR6100_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "VT5100_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(VTDR6100_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "VT5100_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }


        //Gamma保存
        private void VT5100_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + VTDR6100_R0.Text + "," + "G0," + VTDR6100_G0.Text + "," + "B0," + VTDR6100_B0.Text + "\r\n";
                    str = str + "R1," + VTDR6100_R1.Text + "," + "G1," + VTDR6100_G1.Text + "," + "B1," + VTDR6100_B1.Text + "\r\n";
                    str = str + "R3," + VTDR6100_R3.Text + "," + "G3," + VTDR6100_G3.Text + "," + "B3," + VTDR6100_B3.Text + "\r\n";
                    str = str + "R5," + VTDR6100_R5.Text + "," + "G5," + VTDR6100_G5.Text + "," + "B5," + VTDR6100_B5.Text + "\r\n";
                    str = str + "R7," + VTDR6100_R7.Text + "," + "G7," + VTDR6100_G7.Text + "," + "B7," + VTDR6100_B7.Text + "\r\n";
                    str = str + "R11," + VTDR6100_R11.Text + "," + "G11," + VTDR6100_G11.Text + "," + "B11," + VTDR6100_B11.Text + "\r\n";
                    str = str + "R15," + VTDR6100_R15.Text + "," + "G15," + VTDR6100_G15.Text + "," + "B15," + VTDR6100_B15.Text + "\r\n";
                    str = str + "R19," + VTDR6100_R19.Text + "," + "G19," + VTDR6100_G19.Text + "," + "B20," + VTDR6100_B19.Text + "\r\n";
                    str = str + "R23," + VTDR6100_R23.Text + "," + "G23," + VTDR6100_G23.Text + "," + "B23," + VTDR6100_B23.Text + "\r\n";
                    str = str + "R27," + VTDR6100_R27.Text + "," + "G27," + VTDR6100_G27.Text + "," + "B27," + VTDR6100_B27.Text + "\r\n";
                    str = str + "R31," + VTDR6100_R31.Text + "," + "G31," + VTDR6100_G31.Text + "," + "B31," + VTDR6100_B31.Text + "\r\n";
                    str = str + "R35," + VTDR6100_R35.Text + "," + "G35," + VTDR6100_G35.Text + "," + "B35," + VTDR6100_B35.Text + "\r\n";
                    str = str + "R39," + VTDR6100_R39.Text + "," + "G39," + VTDR6100_G39.Text + "," + "B39," + VTDR6100_B39.Text + "\r\n";
                    str = str + "R47," + VTDR6100_R47.Text + "," + "G47," + VTDR6100_G47.Text + "," + "B47," + VTDR6100_B47.Text + "\r\n";
                    str = str + "R55," + VTDR6100_R55.Text + "," + "G55," + VTDR6100_G55.Text + "," + "B55," + VTDR6100_B55.Text + "\r\n";
                    str = str + "R63," + VTDR6100_R63.Text + "," + "G63," + VTDR6100_G63.Text + "," + "B63," + VTDR6100_B63.Text + "\r\n";
                    str = str + "R71," + VTDR6100_R71.Text + "," + "G71," + VTDR6100_G71.Text + "," + "B71," + VTDR6100_B71.Text + "\r\n";
                    str = str + "R79," + VTDR6100_R79.Text + "," + "G79," + VTDR6100_G79.Text + "," + "B79," + VTDR6100_B79.Text + "\r\n";
                    str = str + "R95," + VTDR6100_R95.Text + "," + "G95," + VTDR6100_G95.Text + "," + "B95," + VTDR6100_B95.Text + "\r\n";
                    str = str + "R111," + VTDR6100_R111.Text + "," + "G111," + VTDR6100_G111.Text + "," + "B111," + VTDR6100_B111.Text + "\r\n";
                    str = str + "R127," + VTDR6100_R127.Text + "," + "G127," + VTDR6100_G127.Text + "," + "B127," + VTDR6100_B127.Text + "\r\n";
                    str = str + "R159," + VTDR6100_R159.Text + "," + "G159," + VTDR6100_G159.Text + "," + "B159," + VTDR6100_B159.Text + "\r\n";
                    str = str + "R191," + VTDR6100_R191.Text + "," + "G191," + VTDR6100_G191.Text + "," + "B191," + VTDR6100_B191.Text + "\r\n";
                    str = str + "R223," + VTDR6100_R223.Text + "," + "G223," + VTDR6100_G223.Text + "," + "B223," + VTDR6100_B223.Text + "\r\n";
                    str = str + "R255," + VTDR6100_R255.Text + "," + "G255," + VTDR6100_G255.Text + "," + "B255," + VTDR6100_B255.Text + "\r\n";
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void VT5100_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                VTDR6100_R0.Text = arrtmp[1].ToString(); VTDR6100_G0.Text = arrtmp[3].ToString(); VTDR6100_B0.Text = arrtmp[5].ToString();
                VTDR6100_R1.Text = arrtmp[7].ToString(); VTDR6100_G1.Text = arrtmp[9].ToString(); VTDR6100_B1.Text = arrtmp[11].ToString();
                VTDR6100_R3.Text = arrtmp[13].ToString(); VTDR6100_G3.Text = arrtmp[15].ToString(); VTDR6100_B3.Text = arrtmp[17].ToString();
                VTDR6100_R5.Text = arrtmp[19].ToString(); VTDR6100_G5.Text = arrtmp[21].ToString(); VTDR6100_B5.Text = arrtmp[23].ToString();
                VTDR6100_R7.Text = arrtmp[25].ToString(); VTDR6100_G7.Text = arrtmp[27].ToString(); VTDR6100_B7.Text = arrtmp[29].ToString();
                VTDR6100_R11.Text = arrtmp[31].ToString(); VTDR6100_G11.Text = arrtmp[33].ToString(); VTDR6100_B11.Text = arrtmp[35].ToString();
                VTDR6100_R15.Text = arrtmp[37].ToString(); VTDR6100_G15.Text = arrtmp[39].ToString(); VTDR6100_B15.Text = arrtmp[41].ToString();
                VTDR6100_R19.Text = arrtmp[43].ToString(); VTDR6100_G19.Text = arrtmp[45].ToString(); VTDR6100_B19.Text = arrtmp[47].ToString();
                VTDR6100_R23.Text = arrtmp[49].ToString(); VTDR6100_G23.Text = arrtmp[51].ToString(); VTDR6100_B23.Text = arrtmp[53].ToString();
                VTDR6100_R27.Text = arrtmp[55].ToString(); VTDR6100_G27.Text = arrtmp[57].ToString(); VTDR6100_B27.Text = arrtmp[59].ToString();
                VTDR6100_R31.Text = arrtmp[61].ToString(); VTDR6100_G31.Text = arrtmp[63].ToString(); VTDR6100_B31.Text = arrtmp[65].ToString();
                VTDR6100_R35.Text = arrtmp[67].ToString(); VTDR6100_G35.Text = arrtmp[69].ToString(); VTDR6100_B35.Text = arrtmp[71].ToString();
                VTDR6100_R39.Text = arrtmp[73].ToString(); VTDR6100_G39.Text = arrtmp[75].ToString(); VTDR6100_B39.Text = arrtmp[77].ToString();
                VTDR6100_R47.Text = arrtmp[79].ToString(); VTDR6100_G47.Text = arrtmp[81].ToString(); VTDR6100_B47.Text = arrtmp[83].ToString();
                VTDR6100_R55.Text = arrtmp[85].ToString(); VTDR6100_G55.Text = arrtmp[87].ToString(); VTDR6100_B55.Text = arrtmp[89].ToString();
                VTDR6100_R63.Text = arrtmp[91].ToString(); VTDR6100_G63.Text = arrtmp[93].ToString(); VTDR6100_B63.Text = arrtmp[95].ToString();
                VTDR6100_R71.Text = arrtmp[97].ToString(); VTDR6100_G71.Text = arrtmp[99].ToString(); VTDR6100_B71.Text = arrtmp[101].ToString();
                VTDR6100_R79.Text = arrtmp[103].ToString(); VTDR6100_G79.Text = arrtmp[105].ToString(); VTDR6100_B79.Text = arrtmp[107].ToString();
                VTDR6100_R95.Text = arrtmp[109].ToString(); VTDR6100_G95.Text = arrtmp[111].ToString(); VTDR6100_B95.Text = arrtmp[113].ToString();
                VTDR6100_R111.Text = arrtmp[115].ToString(); VTDR6100_G111.Text = arrtmp[117].ToString(); VTDR6100_B111.Text = arrtmp[119].ToString();
                VTDR6100_R127.Text = arrtmp[121].ToString(); VTDR6100_G127.Text = arrtmp[123].ToString(); VTDR6100_B127.Text = arrtmp[125].ToString();
                VTDR6100_R159.Text = arrtmp[127].ToString(); VTDR6100_G159.Text = arrtmp[129].ToString(); VTDR6100_B159.Text = arrtmp[131].ToString();
                VTDR6100_R191.Text = arrtmp[133].ToString(); VTDR6100_G191.Text = arrtmp[135].ToString(); VTDR6100_B191.Text = arrtmp[137].ToString();
                VTDR6100_R223.Text = arrtmp[139].ToString(); VTDR6100_G223.Text = arrtmp[141].ToString(); VTDR6100_B223.Text = arrtmp[143].ToString();
                VTDR6100_R255.Text = arrtmp[145].ToString(); VTDR6100_G255.Text = arrtmp[147].ToString(); VTDR6100_B255.Text = arrtmp[149].ToString();
            }
        }
        private void VTDR6100_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x4D: VTDR_X110的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x4D && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        //---------------------------------------------------VTDR6100---------------------------------------
                        if (Rec_tmp[0] == 0x4D)      //VTDR6100
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        if (Setting.TempRecord.VT5100CmdType == 0x02)  //register read
                                        {
                                            string tmp = "";
                                            for (int i = 3; i < (Setting.TempRecord.cmdLen - 3); i++)
                                            {
                                                tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";
                                            }
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[Setting.TempRecord.cmdLen - 3]);
                                            VTDR6100_txtRegData.Text = tmp;
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x07:
                                    {
                                    }
                                    break;
                                case 0x09:
                                    {
                                        if (Setting.TempRecord.VT5100CmdType == 0x03)
                                        {
                                            VTDR6100_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            VTDR6100_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            VTDR6100_R3.Value = Decimal.Parse((Rec_tmp[8] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            VTDR6100_R5.Value = Decimal.Parse((Rec_tmp[10] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            VTDR6100_R7.Value = Decimal.Parse((Rec_tmp[12] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            VTDR6100_R11.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            VTDR6100_R15.Value = Decimal.Parse((Rec_tmp[16] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            VTDR6100_R19.Value = Decimal.Parse((Rec_tmp[18] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());

                                            VTDR6100_R23.Value = Decimal.Parse((Rec_tmp[20] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            VTDR6100_R27.Value = Decimal.Parse((Rec_tmp[22] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            VTDR6100_R31.Value = Decimal.Parse((Rec_tmp[24] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            VTDR6100_R35.Value = Decimal.Parse((Rec_tmp[26] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            VTDR6100_R39.Value = Decimal.Parse((Rec_tmp[28] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            VTDR6100_R47.Value = Decimal.Parse((Rec_tmp[30] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            VTDR6100_R55.Value = Decimal.Parse((Rec_tmp[32] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            VTDR6100_R63.Value = Decimal.Parse((Rec_tmp[34] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());

                                            VTDR6100_R71.Value = Decimal.Parse((Rec_tmp[36] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            VTDR6100_R79.Value = Decimal.Parse((Rec_tmp[38] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            VTDR6100_R95.Value = Decimal.Parse((Rec_tmp[40] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            VTDR6100_R111.Value = Decimal.Parse((Rec_tmp[42] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            VTDR6100_R127.Value = Decimal.Parse((Rec_tmp[44] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            VTDR6100_R159.Value = Decimal.Parse((Rec_tmp[46] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            VTDR6100_R191.Value = Decimal.Parse((Rec_tmp[48] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            VTDR6100_R223.Value = Decimal.Parse((Rec_tmp[50] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                            VTDR6100_R255.Value = Decimal.Parse((Rec_tmp[52] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.VT5100CmdType == 0x04)
                                        {
                                            VTDR6100_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            VTDR6100_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            VTDR6100_G3.Value = Decimal.Parse((Rec_tmp[8] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            VTDR6100_G5.Value = Decimal.Parse((Rec_tmp[10] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            VTDR6100_G7.Value = Decimal.Parse((Rec_tmp[12] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            VTDR6100_G11.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            VTDR6100_G15.Value = Decimal.Parse((Rec_tmp[16] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            VTDR6100_G19.Value = Decimal.Parse((Rec_tmp[18] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());

                                            VTDR6100_G23.Value = Decimal.Parse((Rec_tmp[20] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            VTDR6100_G27.Value = Decimal.Parse((Rec_tmp[22] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            VTDR6100_G31.Value = Decimal.Parse((Rec_tmp[24] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            VTDR6100_G35.Value = Decimal.Parse((Rec_tmp[26] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            VTDR6100_G39.Value = Decimal.Parse((Rec_tmp[28] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            VTDR6100_G47.Value = Decimal.Parse((Rec_tmp[30] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            VTDR6100_G55.Value = Decimal.Parse((Rec_tmp[32] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            VTDR6100_G63.Value = Decimal.Parse((Rec_tmp[34] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());

                                            VTDR6100_G71.Value = Decimal.Parse((Rec_tmp[36] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            VTDR6100_G79.Value = Decimal.Parse((Rec_tmp[38] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            VTDR6100_G95.Value = Decimal.Parse((Rec_tmp[40] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            VTDR6100_G111.Value = Decimal.Parse((Rec_tmp[42] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            VTDR6100_G127.Value = Decimal.Parse((Rec_tmp[44] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            VTDR6100_G159.Value = Decimal.Parse((Rec_tmp[46] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            VTDR6100_G191.Value = Decimal.Parse((Rec_tmp[48] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            VTDR6100_G223.Value = Decimal.Parse((Rec_tmp[50] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                            VTDR6100_G255.Value = Decimal.Parse((Rec_tmp[52] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.VT5100CmdType == 0x05)
                                        {
                                            VTDR6100_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            VTDR6100_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            VTDR6100_B3.Value = Decimal.Parse((Rec_tmp[8] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            VTDR6100_B5.Value = Decimal.Parse((Rec_tmp[10] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            VTDR6100_B7.Value = Decimal.Parse((Rec_tmp[12] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            VTDR6100_B11.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            VTDR6100_B15.Value = Decimal.Parse((Rec_tmp[16] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            VTDR6100_B19.Value = Decimal.Parse((Rec_tmp[18] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());

                                            VTDR6100_B23.Value = Decimal.Parse((Rec_tmp[20] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            VTDR6100_B27.Value = Decimal.Parse((Rec_tmp[22] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            VTDR6100_B31.Value = Decimal.Parse((Rec_tmp[24] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            VTDR6100_B35.Value = Decimal.Parse((Rec_tmp[26] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            VTDR6100_B39.Value = Decimal.Parse((Rec_tmp[28] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            VTDR6100_B47.Value = Decimal.Parse((Rec_tmp[30] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            VTDR6100_B55.Value = Decimal.Parse((Rec_tmp[32] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            VTDR6100_B63.Value = Decimal.Parse((Rec_tmp[34] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());

                                            VTDR6100_B71.Value = Decimal.Parse((Rec_tmp[36] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            VTDR6100_B79.Value = Decimal.Parse((Rec_tmp[38] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            VTDR6100_B95.Value = Decimal.Parse((Rec_tmp[40] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            VTDR6100_B111.Value = Decimal.Parse((Rec_tmp[42] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            VTDR6100_B127.Value = Decimal.Parse((Rec_tmp[44] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            VTDR6100_B159.Value = Decimal.Parse((Rec_tmp[46] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            VTDR6100_B191.Value = Decimal.Parse((Rec_tmp[48] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            VTDR6100_B223.Value = Decimal.Parse((Rec_tmp[50] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                            VTDR6100_B255.Value = Decimal.Parse((Rec_tmp[52] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t传输 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t传输 ERROR！\r\n";
                                    }
                                    break;

                                case 0x20:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                    }
                                    break;

                                case 0x21:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Erase Flash Finished \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Erase Flash ERROR \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x22:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Flash IC Status: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                    }
                                    break;

                                case 0x23:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check Flash  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check Flash  \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x24:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Step Five  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Step Five  \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x25:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Five  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Five  \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x26:
                                    {
                                        string tmp = null;
                                        Setting.TempRecord.cmdLen = 14;
                                        for (int i = 3; i < (Setting.TempRecord.cmdLen - 3); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";
                                        }
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[Setting.TempRecord.cmdLen - 3]);
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + tmp + "\n ";

                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check_Flash 完成！\r\n";
                                    }
                                    break;
                                case 0x27:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Viewtrix Demura on  \t OK！\r\n";
                                    }
                                    break;
                                case 0x28:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Viewtrix Demura off  \t OK！\r\n";
                                    }
                                    break;
                                default: break;

                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }

        public void VTDR6100_GAMMA_255fix_Click(byte tep, decimal RGB_Value)
        {
            Send_buf = new byte[57];
            Setting.TempRecord.cmdLen = 57;
            Send_buf[2] = 52;
            Send_buf[3] = 0;
            Send_buf[0] = 0x4D; //VTDR6100

            Send_buf[1] = 0x03;                       //写Gamma,固定255绑点
            Send_buf[4] = (byte)(VTDR6100_Gamma_Group.Value);//gamma_group_select
            if (tep == 0)
            { Send_buf[3] = 0x00; }//红色1
            else if (tep == 1)
            { Send_buf[3] = 0x42; }//绿色1
            else if (tep == 2)
            { Send_buf[3] = 0x81; }

            Send_buf[53] = Convert.ToByte(Convert.ToUInt16((int)(RGB_Value) >> 8));
            Send_buf[54] = Convert.ToByte((Convert.ToUInt16(RGB_Value)) & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[55], out Send_buf[56]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 57);   //VTDR6100
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        public void VTDR6100_GAMMA_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[57];
            Setting.TempRecord.cmdLen = 57;
            Send_buf[2] = 52;
            Send_buf[3] = 0;
            Send_buf[0] = 0x4D; //VTDR6100

            switch (btnTmp.Name)
            {
                case "VTDR6100_GammaR_Red":
                    {
                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] = 0x00;                            //红色red
                        Send_buf[4] = (byte)(VTDR6100_Gamma_Group.Value);
                        Setting.TempRecord.VT5100CmdType = 0x03;
                    }
                    break;
                case "VTDR6100_GammaR_Green":
                    {
                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] = 0x42;                        //绿色 green
                        Send_buf[4] = (byte)(VTDR6100_Gamma_Group.Value);
                        Setting.TempRecord.VT5100CmdType = 0x04;
                    }
                    break;
                case "VTDR6100_GammaR_Blue":
                    {
                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] = 0x81;                        //   blue  
                        Send_buf[4] = (byte)(VTDR6100_Gamma_Group.Value);
                        Setting.TempRecord.VT5100CmdType = 0x05;
                    }
                    break;
                case "VTDR6100_GammaW_Red":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        Send_buf[3] = 0x00;                       //红色1
                        Send_buf[4] = (byte)(VTDR6100_Gamma_Group.Value);//gamma_group_select

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R3.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R3.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R5.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R5.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R7.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R7.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R11.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R11.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R15.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R15.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R19.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R19.Value.ToString())) & 0xff);

                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R23.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R23.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R27.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R27.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R31.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R31.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R35.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R35.Value.ToString())) & 0xff);
                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R39.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R39.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R47.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R47.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R55.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R55.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R63.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R63.Value.ToString())) & 0xff);

                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R71.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R71.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R79.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R79.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R95.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R95.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R111.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R111.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R127.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R127.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R159.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R159.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R191.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R191.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R223.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R223.Value.ToString())) & 0xff);
                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(VTDR6100_R255.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(VTDR6100_R255.Value.ToString())) & 0xff);
                    }
                    break;
                case "VTDR6100_GammaW_Green":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x42;                       //绿色1
                        Send_buf[4] = (byte)(VTDR6100_Gamma_Group.Value);

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G3.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G3.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G5.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G5.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G7.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G7.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G11.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G11.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G15.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G15.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G19.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G19.Value.ToString())) & 0xff);

                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G23.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G23.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G27.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G27.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G31.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G31.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G35.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G35.Value.ToString())) & 0xff);
                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G39.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G39.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G47.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G47.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G55.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G55.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G63.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G63.Value.ToString())) & 0xff);

                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G71.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G71.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G79.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G79.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G95.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G95.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G111.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G111.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G127.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G127.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G159.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G159.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G191.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G191.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G223.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G223.Value.ToString())) & 0xff);
                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(VTDR6100_G255.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(VTDR6100_G255.Value.ToString())) & 0xff);

                    }
                    break;
                case "VTDR6100_GammaW_Blue":     //8
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x81;
                        Send_buf[4] = (byte)(VTDR6100_Gamma_Group.Value);

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B3.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B3.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B5.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B5.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B7.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B7.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B11.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B11.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B15.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B15.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B19.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B19.Value.ToString())) & 0xff);

                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B23.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B23.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B27.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B27.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B31.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B31.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B35.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B35.Value.ToString())) & 0xff);
                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B39.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B39.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B47.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B47.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B55.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B55.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B63.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B63.Value.ToString())) & 0xff);

                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B71.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B71.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B79.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B79.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B95.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B95.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B111.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B111.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B127.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B127.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B159.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B159.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B191.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B191.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B223.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B223.Value.ToString())) & 0xff);
                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(VTDR6100_B255.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(VTDR6100_B255.Value.ToString())) & 0xff);
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[55], out Send_buf[56]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 57);   //VTDR6100
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        public void VTDR6100_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x4D;
            Send_buf[2] = 0x02;

            switch (btnTmp.Name)
            {
                case "VT_OTP": //otp Gamma
                    {
                        Send_buf[1] = 0x0D;
                        if (rbt_otp_p0.Checked == true)
                        { Send_buf[3] = 0x0F; }
                        else if (rbt_otp_p1.Checked == true)
                        { Send_buf[3] = 0x1B; }
                        else if (rbt_otp_p2.Checked == true)
                        { Send_buf[3] = 0x1C; }
                        else if (rbt_otp_p4.Checked == true)
                        { Send_buf[3] = 0x1A; }
                        else if (rbt_otp_p5.Checked == true)
                        { Send_buf[3] = 0x1D; }
                        else if (rbt_otp_p7.Checked == true)
                        { Send_buf[3] = 0x1E; }
                        else if (rbt_otp_p8.Checked == true)
                        { Send_buf[3] = 0x1F; }
                        else if (rbt_otp_all.Checked == true)
                        { Send_buf[3] = 0x0E; }
                        else if (rbt_otp_gamma.Checked == true)
                        { Send_buf[3] = 0x0D; }
                    }
                    break;
                //--------------------------------------------------------------------
                case "Read_Flash_ID":         //读Flash ID
                    {
                        Send_buf[1] = 0x20;
                    }
                    break;
                case "Erase_Flash":         //擦除Flash
                    {
                        Send_buf[1] = 0x21;
                        // frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 50 seconds\r\n";
                    }
                    break;
                case "Flash_Status":         //确认Flash Status
                    {
                        Send_buf[1] = 0x22;
                        //frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 30 seconds\r\n";
                    }
                    break;
                case "Check_Flash":         //确认Flash Status
                    {
                        Send_buf[1] = 0x23;
                        //frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 30 seconds\r\n";
                    }
                    break;
                case "Five_Step":         //确认Flash Status
                    {
                        Send_buf[1] = 0x24;
                        //frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Please Wait for 30 seconds\r\n";
                    }
                    break;
                case "OTP_Flash":         //确认Flash Status
                    {
                        Send_buf[1] = 0x25;
                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Please Wait for 40 seconds\r\n";
                    }
                    break;
                case "CRC_check":
                    {
                        Send_buf[1] = 0x26;
                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "CRC_check\r\n";
                    }
                    break;
                case "Demura_on":
                    {
                        Send_buf[1] = 0x27;
                    }
                    break;
                case "Demura_off":
                    {
                        Send_buf[1] = 0x28;
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        public void VTDR6100_RegWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Int32 DataNum;
            string strLine = Convert.ToString(VTDR6100_txtRegData.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            switch (btnTmp.Name)
            {
                case "VTDR6100_RegW":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x4D;//VTDR6100
                        Send_buf[1] = 0x01;
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "VTDR6100_RegR":
                    {
                        Setting.TempRecord.VT5100CmdType = 0x02;
                        try
                        {
                            DataNum = Convert.ToInt32(VTDR6100_txtDataNum.Text) + 1;
                            Setting.TempRecord.cmdLen = DataNum + 5;
                            Send_buf = new byte[DataNum + 5];
                            Send_buf[0] = 0x4D; //VTDR6100
                            Send_buf[1] = 0x02;
                            Send_buf[2] = byte.Parse(Convert.ToString(DataNum));
                            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());  //cmd
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + DataNum - 2], out Send_buf[5 + DataNum - 1]);
                        }
                        catch
                        {
                            MessageBox.Show("读取长度有误，请检查", "错误");
                            break;
                        }
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + DataNum);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                default: break;
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        public void VTDR6100_trackBar_Scroll(object sender, EventArgs e)
        {
            VTDR6100_label.Text = VTDR6100_51Reg_trackBar.Value.ToString() + "阶";
            VTDR6100_txtRegData.Text = "51,00," + VTDR6100_51Reg_trackBar.Value.ToString("x2");
            VTDR6100_RegWR_Click(VTDR6100_RegW, e);
        }
        public void btnCalculate_VTDR6100_Click(object sender, EventArgs e)
        {
            txtVT_255.Text = (Math.Pow((Parttern_VTDR6100_1D[0] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_223.Text = (Math.Pow((Parttern_VTDR6100_1D[1] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_191.Text = (Math.Pow((Parttern_VTDR6100_1D[2] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_159.Text = (Math.Pow((Parttern_VTDR6100_1D[3] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_127.Text = (Math.Pow((Parttern_VTDR6100_1D[4] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_111.Text = (Math.Pow((Parttern_VTDR6100_1D[5] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_95.Text = (Math.Pow((Parttern_VTDR6100_1D[6] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_79.Text = (Math.Pow((Parttern_VTDR6100_1D[7] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_71.Text = (Math.Pow((Parttern_VTDR6100_1D[8] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_63.Text = (Math.Pow((Parttern_VTDR6100_1D[9] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_55.Text = (Math.Pow((Parttern_VTDR6100_1D[10] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_47.Text = (Math.Pow((Parttern_VTDR6100_1D[11] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_39.Text = (Math.Pow((Parttern_VTDR6100_1D[12] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_35.Text = (Math.Pow((Parttern_VTDR6100_1D[13] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_31.Text = (Math.Pow((Parttern_VTDR6100_1D[14] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_27.Text = (Math.Pow((Parttern_VTDR6100_1D[15] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_23.Text = (Math.Pow((Parttern_VTDR6100_1D[16] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_19.Text = (Math.Pow((Parttern_VTDR6100_1D[17] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_15.Text = (Math.Pow((Parttern_VTDR6100_1D[18] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_11.Text = (Math.Pow((Parttern_VTDR6100_1D[19] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_7.Text = (Math.Pow((Parttern_VTDR6100_1D[20] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_5.Text = (Math.Pow((Parttern_VTDR6100_1D[21] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_3.Text = (Math.Pow((Parttern_VTDR6100_1D[22] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");
            txtVT_1.Text = (Math.Pow((Parttern_VTDR6100_1D[23] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text)).ToString("F4");

            if (rbt_vt5100_p15.Checked == true)
            {
                txtVT_255.BackColor = Color.Red;
                txtVT_223.BackColor = Color.Red;
                txtVT_191.BackColor = Color.Red;
                txtVT_127.BackColor = Color.Red;
                txtVT_111.BackColor = Color.Red;
                txtVT_79.BackColor = Color.Red;
                txtVT_63.BackColor = Color.Red;
                txtVT_55.BackColor = Color.Red;
                txtVT_47.BackColor = Color.Red;
                txtVT_39.BackColor = Color.Red;
                txtVT_35.BackColor = Color.Red;
                txtVT_31.BackColor = Color.Red;
                txtVT_11.BackColor = Color.Red;
                txtVT_3.BackColor = Color.Red;
            }
            else
            {
                txtVT_255.BackColor = Color.White;
                txtVT_223.BackColor = Color.White;
                txtVT_191.BackColor = Color.White;
                txtVT_127.BackColor = Color.White;
                txtVT_111.BackColor = Color.White;
                txtVT_79.BackColor = Color.White;
                txtVT_63.BackColor = Color.White;
                txtVT_55.BackColor = Color.White;
                txtVT_47.BackColor = Color.White;
                txtVT_39.BackColor = Color.White;
                txtVT_35.BackColor = Color.White;
                txtVT_31.BackColor = Color.White;
                txtVT_11.BackColor = Color.White;
                txtVT_3.BackColor = Color.White;
            }
        }
        public void btnVTStop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
        }

        public double[] Target_VTDR6100_Lum = new double[14] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
        public double[] Target_VTDR6100_X = new double[14] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
        public double[] Target_VTDR6100_Y = new double[14] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
        public void btn6Gammas_VTDR6100_DLL_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            int VT_Flag = 0;
            int i;
            Class1 gma = new Class1();
            int[] Rcode = new int[14] {Convert.ToInt32(VTDR6100_R3.Text, 16),Convert.ToInt32(VTDR6100_R11.Text, 16),Convert.ToInt32(VTDR6100_R31.Text, 16),Convert.ToInt32(VTDR6100_R35.Text, 16),Convert.ToInt32(VTDR6100_R39.Text, 16),Convert.ToInt32(VTDR6100_R47.Text, 16),
                                       Convert.ToInt32(VTDR6100_R55.Text, 16), Convert.ToInt32(VTDR6100_R63.Text, 16) ,Convert.ToInt32(VTDR6100_R79.Text, 16),Convert.ToInt32(VTDR6100_R111.Text, 16), Convert.ToInt32(VTDR6100_R127.Text, 16) ,
                                       Convert.ToInt32(VTDR6100_R191.Text, 16),Convert.ToInt32(VTDR6100_R223.Text, 16),Convert.ToInt32(VTDR6100_R255.Text, 16) };

            int[] Gcode = new int[14] {Convert.ToInt32(VTDR6100_G3.Text, 16),Convert.ToInt32(VTDR6100_G11.Text, 16),Convert.ToInt32(VTDR6100_G31.Text, 16),Convert.ToInt32(VTDR6100_G35.Text, 16),Convert.ToInt32(VTDR6100_G39.Text, 16),Convert.ToInt32(VTDR6100_G47.Text, 16),
                                       Convert.ToInt32(VTDR6100_G55.Text, 16), Convert.ToInt32(VTDR6100_G63.Text, 16) ,Convert.ToInt32(VTDR6100_G79.Text, 16),Convert.ToInt32(VTDR6100_G111.Text, 16), Convert.ToInt32(VTDR6100_G127.Text, 16) ,
                                       Convert.ToInt32(VTDR6100_G191.Text, 16),Convert.ToInt32(VTDR6100_G223.Text, 16),Convert.ToInt32(VTDR6100_G255.Text, 16) };

            int[] Bcode = new int[14] {Convert.ToInt32(VTDR6100_B3.Text, 16),Convert.ToInt32(VTDR6100_B11.Text, 16),Convert.ToInt32(VTDR6100_B31.Text, 16),Convert.ToInt32(VTDR6100_B35.Text, 16),Convert.ToInt32(VTDR6100_B39.Text, 16),Convert.ToInt32(VTDR6100_B47.Text, 16),
                                       Convert.ToInt32(VTDR6100_B55.Text, 16), Convert.ToInt32(VTDR6100_B63.Text, 16) ,Convert.ToInt32(VTDR6100_B79.Text, 16),Convert.ToInt32(VTDR6100_B111.Text, 16), Convert.ToInt32(VTDR6100_B127.Text, 16) ,
                                       Convert.ToInt32(VTDR6100_B191.Text, 16),Convert.ToInt32(VTDR6100_B223.Text, 16),Convert.ToInt32(VTDR6100_B255.Text, 16)  };

            double[] Lcode = new double[14];
            double[] xcode = new double[14];
            double[] ycode = new double[14];
            Array.Reverse(Target_VTDR6100_Lum);
            Array.Reverse(Target_VTDR6100_X);
            Array.Reverse(Target_VTDR6100_Y);
            Target_VTDR6100_Lum.CopyTo(Lcode, 0);
            Target_VTDR6100_X.CopyTo(xcode, 0);
            Target_VTDR6100_Y.CopyTo(ycode, 0);


            //----------------------------------------------------------------------
            // Write adjusted register settings
            //----------------------------------------------------------------------
            // Create Output Text File
            /*
            System.IO.StreamWriter sw1 = new System.IO.StreamWriter("viewtrix_adjusted_register.txt");
            sw1.AutoFlush = true;
            System.IO.TextWriter tw = System.IO.TextWriter.Synchronized(sw1);
            Console.SetOut(tw);

            Console.Write("==============================Senser_Measure============================\n");
            for (i = 0; i < 14; i++)
            {
                Console.Write("Viewtrix_GAMMA_GRAY={0};     LUM={1}     X={2};    Y={3}\n", Parttern_VTDR6100[13 - i], Target_VTDR6100_Lum[i], Target_VTDR6100_X[i], Target_VTDR6100_Y[i]);
            }

            Console.Write("==================================INPUT=================================\n");
            for (i = 0; i < 14; i++)
            {
                Console.Write("NUM[{0}]:    Rcode[{0}]: {1};    Gcode[{0}]: {2};    Bcode[{0}]: {3};\n", i,  Rcode[i], Gcode[i], Bcode[i]);
            }

            Console.Write("==================================END=================================\n");
            */
            //--------------------------------------------------------------------------------------
            int[,] RGB_Code = new int[3, 25];
            double[,] RGB_Lxy = new double[3, 25];

            for (i = 0; i < 14; i++)
            {
                RGB_Code[0, i] = Rcode[i];
                RGB_Code[1, i] = Gcode[i];
                RGB_Code[2, i] = Bcode[i];

                RGB_Lxy[0, i] = Lcode[i];
                RGB_Lxy[1, i] = xcode[i];
                RGB_Lxy[2, i] = ycode[i];
            }
            int totalpoint = 14;
            int HBMmodebyGma = Convert.ToUInt16(text_HBMmodebyGma.Text);
            double NormalLum = Convert.ToDouble(text_NormalLum.Text);
            int LowLumbyGma = Convert.ToUInt16(text_LowLumbyGma.Text);
            int maxdbv = Convert.ToUInt16(text_Maxdbv.Text);

            //initail first
            //NormalLum is the luminance for dbv=255, HBMmodebyGma need to be set 1
            //gma.GMA_Tool_init(RGB_Code, RGB_Lxy, point5or6, NormalLum, HBMmodebyGma);
            gma.GMA_Tool_init(RGB_Code, RGB_Lxy, totalpoint, maxdbv, NormalLum, HBMmodebyGma, LowLumbyGma);

            int[,] GMA_CodeR = new int[9, 25];
            int[,] GMA_CodeG = new int[9, 25];
            int[,] GMA_CodeB = new int[9, 25];
            int[] dbvpoint = new int[11];
            int gmanumber = 0;

            int[,] GMA_CodeR22 = new int[9, 25];
            int[,] GMA_CodeG22 = new int[9, 25];
            int[,] GMA_CodeB22 = new int[9, 25];

            //for AOD luminance gamma code
            int codeR = 0, codeG = 0, codeB = 0;

            if (rbt_VT_DLL_NORMAL.Checked == true)
            {
                gma.GMA_process(ref GMA_CodeR, ref GMA_CodeG, ref GMA_CodeB, ref dbvpoint, ref gmanumber);
                VT_Flag = 1;
            }
            else if (rbt_VT_Gamma2_2.Checked == true)
            {
                gma.GMA_process(ref GMA_CodeR, ref GMA_CodeG, ref GMA_CodeB, ref dbvpoint, ref gmanumber);
                //use the same dbvpoint and gamma number
                gma.GMA_process2_2(dbvpoint, gmanumber, ref GMA_CodeR22, ref GMA_CodeG22, ref GMA_CodeB22, maxdbv, NormalLum);
                VT_Flag = 2;
            }
            else if (rbt_VT_AOD_LUM.Checked == true)
            {
                gma.GMA_process(ref GMA_CodeR, ref GMA_CodeG, ref GMA_CodeB, ref dbvpoint, ref gmanumber);
                gma.GMA_CodebyLum(30, ref codeR, ref codeG, ref codeB);
                VT_Flag = 3;
            }

            if ((VT_Flag == 1) || (VT_Flag == 2) || (VT_Flag == 3))
            {
                SaveFileDialog sf = new SaveFileDialog(); //对话框
                sf.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //定义保存的文件的类型
                string str = "保存VT Driver IC Gamma Code文件";
                if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                {
                    if (sf.FileName == "") return; //如果没有输入文件名
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    try
                    {
                        //printf 原始的初始化code
                        for (i = 0; i < (frmHelp.lstMIPIDriver.Items.Count - 2); i++)     //IC 初始化代码
                        {
                            str = frmHelp.lstMIPIDriver.Items[i].ToString();
                            sw.WriteLine(str);
                        }
                        if (VT_Flag == 1)
                        {
                            str = "F0,AA,11";
                            sw.WriteLine(str);
                            str = "C3," + (dbvpoint[0] & 0xFF).ToString("X2") + "," + (dbvpoint[1] & 0xFF).ToString("X2") + "," + (dbvpoint[2] & 0xFF).ToString("X2") + ","
                                       + (dbvpoint[3] & 0xFF).ToString("X2") + "," + (dbvpoint[4] & 0xFF).ToString("X2") + "," + (dbvpoint[5] & 0xFF).ToString("X2") + ","
                                       + (dbvpoint[6] & 0xFF).ToString("X2") + "," + (dbvpoint[7] & 0xFF).ToString("X2") + "," + (dbvpoint[8] & 0xFF).ToString("X2") + ","
                                       + (dbvpoint[9] & 0xFF).ToString("X2") + "," + (dbvpoint[10] & 0xFF).ToString("X2") + ","
                                       + (((dbvpoint[0] & 0x100) >> 1) + ((dbvpoint[1] & 0x100) >> 2) + ((dbvpoint[2] & 0x100) >> 3) + ((dbvpoint[3] & 0x100) >> 4) + ((dbvpoint[4] & 0x100) >> 5)
                                       + ((dbvpoint[5] & 0x100) >> 6) + ((dbvpoint[6] & 0x100) >> 7) + ((dbvpoint[7] & 0x100) >> 8)).ToString("X2") + ","
                                       + (((dbvpoint[8] & 0x100) >> 6) + ((dbvpoint[9] & 0x100) >> 7) + ((dbvpoint[10] & 0x100) >> 8)).ToString("X2");
                            sw.WriteLine(str);

                            str = "F0,AA,13";
                            sw.WriteLine(str);

                            for (i = 0; i <= 7; i++)
                            {
                                if ((i > gmanumber) && (i != 7))
                                    continue;

                                str = "BF," + i.ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma R
                                str = "B0,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeR[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                int leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B1,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeR[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B2,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeR[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B3," + ((GMA_CodeR[i, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[i, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma G
                                str = "B4,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeG[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B5,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeG[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B6,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeG[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B7," + ((GMA_CodeG[i, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[i, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma B
                                str = "B8,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeB[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B9,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeB[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "BA,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeB[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "BB," + ((GMA_CodeB[i, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[i, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);



                            }
                            if (HBMmodebyGma == 0)
                            {
                                str = "BF," + "08";
                                sw.WriteLine(str);

                                str = "B0,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeR[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                int leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B1,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeR[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B2,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeR[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B3," + ((GMA_CodeR[7, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[7, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma G
                                str = "B4,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeG[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B5,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeG[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B6,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeG[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B7," + ((GMA_CodeG[7, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[7, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma B
                                str = "B8,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeB[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B9,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeB[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "BA,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeB[7, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[7, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "BB," + ((GMA_CodeB[7, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[7, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                            }
                            if (HBMmodebyGma == 1)
                            {
                                str = "BF," + "08";
                                sw.WriteLine(str);

                                str = "B0,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeR[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                int leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B1,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeR[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B2,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeR[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B3," + ((GMA_CodeR[8, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR[8, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma G
                                str = "B4,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeG[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B5,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeG[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B6,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeG[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B7," + ((GMA_CodeG[8, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG[8, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma B
                                str = "B8,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeB[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "B9,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeB[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "BA,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeB[8, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[8, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng2 = str.Length;
                                str = str.Substring(0, leng2 - 1);
                                sw.WriteLine(str);

                                str = "BB," + ((GMA_CodeB[8, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB[8, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                            }
                            str = "11";
                            sw.WriteLine(str);
                            str = "29";
                            sw.WriteLine(str);
                            sw.Dispose(); //释放资源
                            sw.Close(); //关闭数据流
                            VT_Flag = 0;
                            MessageBox.Show("VT Driver IC Gamma Code保存OK！", "系统提示：");
                        }
                        else if (VT_Flag == 2)
                        {
                            str = "F0,AA,11";
                            sw.WriteLine(str);
                            str = "C3," + (dbvpoint[0] & 0xFF).ToString("X2") + "," + (dbvpoint[1] & 0xFF).ToString("X2") + "," + (dbvpoint[2] & 0xFF).ToString("X2") + ","
                                       + (dbvpoint[3] & 0xFF).ToString("X2") + "," + (dbvpoint[4] & 0xFF).ToString("X2") + "," + (dbvpoint[5] & 0xFF).ToString("X2") + ","
                                       + (dbvpoint[6] & 0xFF).ToString("X2") + "," + (dbvpoint[7] & 0xFF).ToString("X2") + "," + (dbvpoint[8] & 0xFF).ToString("X2") + ","
                                       + (dbvpoint[9] & 0xFF).ToString("X2") + "," + (dbvpoint[10] & 0xFF).ToString("X2") + ","
                                       + (((dbvpoint[0] & 0x100) >> 1) + ((dbvpoint[1] & 0x100) >> 2) + ((dbvpoint[2] & 0x100) >> 3) + ((dbvpoint[3] & 0x100) >> 4) + ((dbvpoint[4] & 0x100) >> 5)
                                       + ((dbvpoint[5] & 0x100) >> 6) + ((dbvpoint[6] & 0x100) >> 7) + ((dbvpoint[7] & 0x100) >> 8)).ToString("X2") + ","
                                       + (((dbvpoint[8] & 0x100) >> 6) + ((dbvpoint[9] & 0x100) >> 7) + ((dbvpoint[10] & 0x100) >> 8)).ToString("X2");
                            sw.WriteLine(str);

                            str = "F0,AA,13";
                            sw.WriteLine(str);

                            for (i = 0; i <= 7; i++)
                            {
                                if ((i > gmanumber) && (i != 7))
                                    continue;

                                str = "BF," + i.ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma R
                                str = "B0,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeR22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                int leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B1,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeR22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B2,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeR22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B3," + ((GMA_CodeR22[i, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeR22[i, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma G
                                str = "B4,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeG22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B5,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeG22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B6,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeG22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B7," + ((GMA_CodeG22[i, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeG22[i, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                                //printf Gamma B
                                str = "B8,";
                                for (int j = 0; j < 8; j++)
                                {
                                    str = str + ((GMA_CodeB22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "B9,";
                                for (int j = 8; j < 16; j++)
                                {
                                    str = str + ((GMA_CodeB22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "BA,";
                                for (int j = 16; j < 24; j++)
                                {
                                    str = str + ((GMA_CodeB22[i, j] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB22[i, j] & 0xFF).ToString("X2") + ",";
                                }
                                leng = str.Length;
                                str = str.Substring(0, leng - 1);
                                sw.WriteLine(str);

                                str = "BB," + ((GMA_CodeB22[i, 24] & 0x300) >> 8).ToString("X2") + "," + (GMA_CodeB22[i, 24] & 0xFF).ToString("X2");
                                sw.WriteLine(str);

                            }
                            str = "11";
                            sw.WriteLine(str);
                            str = "29";
                            sw.WriteLine(str);
                            sw.Dispose(); //释放资源
                            sw.Close(); //关闭数据流
                            VT_Flag = 0;
                            MessageBox.Show("VT Driver IC Gamma Code保存OK！", "系统提示：");
                        }
                        else
                        {
                            str = "F0,AA,13";
                            sw.WriteLine(str);

                            str = "C0," + "00," + "00,";
                            str = str + ((codeR & 0x300) >> 8).ToString("X2") + "," + (codeR & 0xFF).ToString("X2") + ",";
                            str = str + "00," + "00," + "00," + "00";
                            sw.WriteLine(str);

                            str = "C1," + "00," + "00,";
                            str = str + ((codeG & 0x300) >> 8).ToString("X2") + "," + (codeG & 0xFF).ToString("X2") + ",";
                            str = str + "00," + "00," + "00," + "00";
                            sw.WriteLine(str);

                            str = "C2," + "00," + "00,";
                            str = str + ((codeB & 0x300) >> 8).ToString("X2") + "," + (codeB & 0xFF).ToString("X2") + ",";
                            str = str + "00," + "00," + "00," + "00";
                            sw.WriteLine(str);
                            str = "11";
                            sw.WriteLine(str);
                            str = "29";
                            sw.WriteLine(str);
                            sw.Dispose(); //释放资源
                            sw.Close(); //关闭数据流
                            VT_Flag = 0;
                            MessageBox.Show("VT Driver IC Gamma Code保存OK！", "系统提示：");
                        }
                    }
                    catch
                    {
                        MessageBox.Show("VT Driver IC Gamma Code保存时发生错误，请再试！", "系统提示：");
                    }
                }
            }

        }
        public void VT_DLL_NORMAL_Click(object sender, EventArgs e)
        {

        }

        private void T20_Tick(object sender, EventArgs e)
        {
            if (rbt_vt5100_p15.Checked == true)
            {
                VTDR6100_dll_gamma(null, null);
            }
            else
            {
                VTDR6100_dll_gamma_25P(null, null);
            }
        }

        private void rbt_vt5100_p15_CheckedChanged(object sender, EventArgs e)
        {
            if (rbt_vt5100_p15.Checked == true)
            {
                VT5100_Gammadll_group.Enabled = true;
            }
            else
            {
                VT5100_Gammadll_group.Enabled = false;
            }
        }

        public byte VTDR6100_Fix255G_Flag = 0;
        public void btnAutoGammas_VTDR6100_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[14, 3];
            D_lum = new double[14];
            int point = 14;
            Button btnTmp = (Button)sender;
            switch (btnTmp.Name)
            {
                case "btnAutoGammas_VTDR6100": VTDR6100_Fix255G_Flag = 0; break;//otp Gamma
                case "btnAutoGammas_VTDR6100_Fix255G": VTDR6100_Fix255G_Flag = 1; break;//otp Gamma
            }
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;

            if (rbt_vt5100_p15.Checked == true)
            {
                D_lum[0] = Convert.ToDouble(txtVT_255.Text);
                D_lum[1] = Convert.ToDouble(txtVT_223.Text);
                D_lum[2] = Convert.ToDouble(txtVT_191.Text);
                D_lum[3] = Convert.ToDouble(txtVT_127.Text);
                D_lum[4] = Convert.ToDouble(txtVT_111.Text);
                D_lum[5] = Convert.ToDouble(txtVT_79.Text);
                D_lum[6] = Convert.ToDouble(txtVT_63.Text);
                D_lum[7] = Convert.ToDouble(txtVT_55.Text);
                D_lum[8] = Convert.ToDouble(txtVT_47.Text);
                D_lum[9] = Convert.ToDouble(txtVT_39.Text);
                D_lum[10] = Convert.ToDouble(txtVT_35.Text);
                D_lum[11] = Convert.ToDouble(txtVT_31.Text);
                D_lum[12] = Convert.ToDouble(txtVT_11.Text);
                D_lum[13] = Convert.ToDouble(txtVT_3.Text);
                //DLL 方式下，不调整的帮点前期赋值为“0”
                VTDR6100_R159.Value = 0; VTDR6100_G159.Value = 0; VTDR6100_B159.Value = 0;
                VTDR6100_R95.Value = 0; VTDR6100_G95.Value = 0; VTDR6100_B95.Value = 0;
                VTDR6100_R71.Value = 0; VTDR6100_G71.Value = 0; VTDR6100_B71.Value = 0;
                VTDR6100_R27.Value = 0; VTDR6100_G27.Value = 0; VTDR6100_B27.Value = 0;
                VTDR6100_R23.Value = 0; VTDR6100_G23.Value = 0; VTDR6100_B23.Value = 0;
                VTDR6100_R19.Value = 0; VTDR6100_G19.Value = 0; VTDR6100_B19.Value = 0;
                VTDR6100_R15.Value = 0; VTDR6100_G15.Value = 0; VTDR6100_B15.Value = 0;
                VTDR6100_R7.Value = 0; VTDR6100_G7.Value = 0; VTDR6100_B7.Value = 0;
                VTDR6100_R5.Value = 0; VTDR6100_G5.Value = 0; VTDR6100_B5.Value = 0;
                VTDR6100_R1.Value = 0; VTDR6100_G1.Value = 0; VTDR6100_B1.Value = 0;
            }
            else
            {
                point = 24;
                D_lums = new double[24, 3];
                D_lum = new double[24];
                D_lum[0] = Convert.ToDouble(txtVT_255.Text);
                D_lum[1] = Convert.ToDouble(txtVT_223.Text);
                D_lum[2] = Convert.ToDouble(txtVT_191.Text);
                D_lum[3] = Convert.ToDouble(txtVT_159.Text);
                D_lum[4] = Convert.ToDouble(txtVT_127.Text);
                D_lum[5] = Convert.ToDouble(txtVT_111.Text);
                D_lum[6] = Convert.ToDouble(txtVT_95.Text);
                D_lum[7] = Convert.ToDouble(txtVT_79.Text);
                D_lum[8] = Convert.ToDouble(txtVT_71.Text);
                D_lum[9] = Convert.ToDouble(txtVT_63.Text);
                D_lum[10] = Convert.ToDouble(txtVT_55.Text);
                D_lum[11] = Convert.ToDouble(txtVT_47.Text);
                D_lum[12] = Convert.ToDouble(txtVT_39.Text);
                D_lum[13] = Convert.ToDouble(txtVT_35.Text);
                D_lum[14] = Convert.ToDouble(txtVT_31.Text);
                D_lum[15] = Convert.ToDouble(txtVT_27.Text);
                D_lum[16] = Convert.ToDouble(txtVT_23.Text);
                D_lum[17] = Convert.ToDouble(txtVT_19.Text);
                D_lum[18] = Convert.ToDouble(txtVT_15.Text);
                D_lum[19] = Convert.ToDouble(txtVT_11.Text);
                D_lum[20] = Convert.ToDouble(txtVT_7.Text);
                D_lum[21] = Convert.ToDouble(txtVT_5.Text);
                D_lum[22] = Convert.ToDouble(txtVT_3.Text);
                D_lum[23] = Convert.ToDouble(txtVT_1.Text);
            }


            D_x = Convert.ToDouble(txtVT_x.Text);
            D_y = Convert.ToDouble(txtVT_y.Text);

            CaSDKObj.YUV_to_RGB(D_y, D_x, out Lr_d, out Lb_d, out Lg_d);

            for (int i = 0; i < point; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txtVT_loff.Text);
                        x_off = Convert.ToDouble(txtVT_xoff.Text);
                        y_off = Convert.ToDouble(txtVT_yoff.Text);
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);
                        Thread.Sleep(10);
                        T20.Enabled = true;
                    }
                    break;
                case 2:                                           //寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        public void VTDR6100_dll_gamma(Object myObject, EventArgs myEventArgs)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;

            frmHelp.Msr_CASDK();

            if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
                flag1 = 0;
            if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
                flag2 = 0;
            if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;
                if (index == 14)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "VTDR6100 gamma调试完毕!\r\n";
                }
                if (index < 14)
                {
                    switch (index)
                    {
                        case 0:
                            lum_off = (Convert.ToDouble(txtVT_255.Text) * Convert.ToDouble(textVT_255_off.Text)) / 100;
                            break;
                        case 1:
                            lum_off = (Convert.ToDouble(txtVT_223.Text) * Convert.ToDouble(textVT_223_off.Text)) / 100;
                            break;
                        case 2:
                            lum_off = (Convert.ToDouble(txtVT_191.Text) * Convert.ToDouble(textVT_191_off.Text)) / 100;
                            break;
                        case 3:
                            lum_off = (Convert.ToDouble(txtVT_127.Text) * Convert.ToDouble(textVT_127_off.Text)) / 100;
                            break;
                        case 4:
                            lum_off = (Convert.ToDouble(txtVT_111.Text) * Convert.ToDouble(textVT_111_off.Text)) / 100;
                            break;
                        case 5:
                            lum_off = (Convert.ToDouble(txtVT_79.Text) * Convert.ToDouble(textVT_79_off.Text)) / 100;
                            break;
                        case 6:
                            lum_off = (Convert.ToDouble(txtVT_63.Text) * Convert.ToDouble(textVT_63_off.Text)) / 100;
                            break;
                        case 7:
                            lum_off = (Convert.ToDouble(txtVT_55.Text) * Convert.ToDouble(textVT_55_off.Text)) / 100;
                            break;
                        case 8:
                            lum_off = (Convert.ToDouble(txtVT_47.Text) * Convert.ToDouble(textVT_47_off.Text)) / 100;
                            break;
                        case 9:
                            lum_off = (Convert.ToDouble(txtVT_39.Text) * Convert.ToDouble(textVT_39_off.Text)) / 100;
                            break;
                        case 10:
                            lum_off = (Convert.ToDouble(txtVT_35.Text) * Convert.ToDouble(textVT_35_off.Text)) / 100;
                            break;
                        case 11:
                            lum_off = (Convert.ToDouble(txtVT_31.Text) * Convert.ToDouble(textVT_31_off.Text)) / 100;
                            break;
                        case 12:
                            {
                                lum_off = (Convert.ToDouble(txtVT_11.Text) * Convert.ToDouble(textVT_11_off.Text)) / 100;
                            }
                            break;
                        case 13:
                            {
                                lum_off = (Convert.ToDouble(txtVT_3.Text) * Convert.ToDouble(textVT_3_off.Text)) / 100;
                            }
                            break;
                    }
                    if (VTDR6100_Fix255G_Flag == 1)
                    {
                        frmHelp.numPatternR.Value = Parttern_VTDR6100[0];//固定255灰阶
                        frmHelp.numPatternG.Value = Parttern_VTDR6100[0];
                        frmHelp.numPatternB.Value = Parttern_VTDR6100[0];
                    }
                    else
                    {
                        frmHelp.numPatternR.Value = Parttern_VTDR6100[index];//固定255灰阶
                        frmHelp.numPatternG.Value = Parttern_VTDR6100[index];
                        frmHelp.numPatternB.Value = Parttern_VTDR6100[index];
                    }
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                int flexible_gamma_count = 120;

                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R255.Value, VTDR6100_G255.Value, VTDR6100_B255.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R255.Value = r;
                                VTDR6100_G255.Value = g;
                                VTDR6100_B255.Value = b;

                                if (VTDR6100_G255.Value <= VTDR6100_G223.Value)
                                {
                                    VTDR6100_R223.Value = r - 20; VTDR6100_G223.Value = g - 20; VTDR6100_B223.Value = b - 20;
                                }
                                // auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692AX_R32.Value), gamma_g[index], Convert.ToUInt16(RM692AX_G32.Value), gamma_b[index], Convert.ToUInt16(RM692AX_B32.Value));
                            }
                            break;
                        case 1:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R223.Value, VTDR6100_G223.Value, VTDR6100_B223.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R223.Value = r;
                                VTDR6100_G223.Value = g;
                                VTDR6100_B223.Value = b;
                                if (VTDR6100_G223.Value <= VTDR6100_G191.Value)
                                {
                                    VTDR6100_R191.Value = r - 20; VTDR6100_G191.Value = g - 20; VTDR6100_B191.Value = b - 20;
                                }
                            }
                            break;
                        case 2:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R191.Value, VTDR6100_G191.Value, VTDR6100_B191.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R191.Value = r;
                                VTDR6100_G191.Value = g;
                                VTDR6100_B191.Value = b;
                                if (VTDR6100_G191.Value <= VTDR6100_G127.Value)
                                {
                                    VTDR6100_R127.Value = r - 20; VTDR6100_G127.Value = g - 20; VTDR6100_B127.Value = b - 20;
                                }
                            }
                            break;

                        case 3:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R127.Value, VTDR6100_G127.Value, VTDR6100_B127.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R127.Value = r;
                                VTDR6100_G127.Value = g;
                                VTDR6100_B127.Value = b;
                                if (VTDR6100_G127.Value <= VTDR6100_G111.Value)
                                {
                                    VTDR6100_R111.Value = r - 20; VTDR6100_G111.Value = g - 20; VTDR6100_B111.Value = b - 20;
                                }
                            }
                            break;

                        case 4:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R111.Value, VTDR6100_G111.Value, VTDR6100_B111.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R111.Value = r;
                                VTDR6100_G111.Value = g;
                                VTDR6100_B111.Value = b;
                                if (VTDR6100_G111.Value <= VTDR6100_G79.Value)
                                {
                                    VTDR6100_R79.Value = r - 20; VTDR6100_G79.Value = g - 20; VTDR6100_B79.Value = b - 20;
                                }
                            }
                            break;

                        case 5:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R79.Value, VTDR6100_G79.Value, VTDR6100_B79.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R79.Value = r;
                                VTDR6100_G79.Value = g;
                                VTDR6100_B79.Value = b;
                                if (VTDR6100_G79.Value <= VTDR6100_G63.Value)
                                {
                                    VTDR6100_R63.Value = r - 20; VTDR6100_G63.Value = g - 20; VTDR6100_B63.Value = b - 20;
                                }
                            }
                            break;

                        case 6:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R63.Value, VTDR6100_G63.Value, VTDR6100_B63.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R63.Value = r;
                                VTDR6100_G63.Value = g;
                                VTDR6100_B63.Value = b;
                                if (VTDR6100_G63.Value <= VTDR6100_G55.Value)
                                {
                                    VTDR6100_R55.Value = r - 20; VTDR6100_G55.Value = g - 20; VTDR6100_B55.Value = b - 20;
                                }
                            }
                            break;

                        case 7:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R55.Value, VTDR6100_G55.Value, VTDR6100_B55.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R55.Value = r;
                                VTDR6100_G55.Value = g;
                                VTDR6100_B55.Value = b;
                                if (VTDR6100_G55.Value <= VTDR6100_G47.Value)
                                {
                                    VTDR6100_R47.Value = r - 20; VTDR6100_G47.Value = g - 20; VTDR6100_B47.Value = b - 20;
                                }
                            }
                            break;

                        case 8:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R47.Value, VTDR6100_G47.Value, VTDR6100_B47.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R47.Value = r;
                                VTDR6100_G47.Value = g;
                                VTDR6100_B47.Value = b;
                                if (VTDR6100_G47.Value <= VTDR6100_G39.Value)
                                {
                                    VTDR6100_R39.Value = r - 20; VTDR6100_G39.Value = g - 20; VTDR6100_B39.Value = b - 20;
                                }
                            }
                            break;

                        case 9:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R39.Value, VTDR6100_G39.Value, VTDR6100_B39.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R39.Value = r;
                                VTDR6100_G39.Value = g;
                                VTDR6100_B39.Value = b;
                                if (VTDR6100_G39.Value <= VTDR6100_G35.Value)
                                {
                                    VTDR6100_R35.Value = r - 20; VTDR6100_G35.Value = g - 20; VTDR6100_B35.Value = b - 20;
                                }
                            }
                            break;

                        case 10:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R35.Value, VTDR6100_G35.Value, VTDR6100_B35.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R35.Value = r;
                                VTDR6100_G35.Value = g;
                                VTDR6100_B35.Value = b;
                                if (VTDR6100_G35.Value <= VTDR6100_G31.Value)
                                {
                                    VTDR6100_R31.Value = r - 20; VTDR6100_G31.Value = g - 20; VTDR6100_B31.Value = b - 20;
                                }
                            }
                            break;

                        case 11:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R31.Value, VTDR6100_G31.Value, VTDR6100_B31.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R31.Value = r;
                                VTDR6100_G31.Value = g;
                                VTDR6100_B31.Value = b;
                                if (VTDR6100_G31.Value <= VTDR6100_G11.Value)
                                {
                                    VTDR6100_R11.Value = r - 20; VTDR6100_G11.Value = g - 20; VTDR6100_B11.Value = b - 20;
                                }
                            }
                            break;

                        case 12:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R11.Value, VTDR6100_G11.Value, VTDR6100_B11.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R11.Value = r;
                                VTDR6100_G11.Value = g;
                                VTDR6100_B11.Value = b;
                                if (VTDR6100_G11.Value <= VTDR6100_G3.Value)
                                {
                                    VTDR6100_R3.Value = r - 20; VTDR6100_G3.Value = g - 20; VTDR6100_B3.Value = b - 20;
                                }
                            }
                            break;
                        case 13:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R3.Value, VTDR6100_G3.Value, VTDR6100_B3.Value, Parttern_VTDR6100[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R3.Value = r;
                                VTDR6100_G3.Value = g;
                                VTDR6100_B3.Value = b;
                            }
                            break;
                        default:
                            break;
                    }
                    //   if (index < 13)
                    //   {
                    Target_VTDR6100_Lum[index] = frmHelp.CASDK_LUM;
                    Target_VTDR6100_X[index] = frmHelp.CASDK_x;
                    Target_VTDR6100_Y[index] = frmHelp.CASDK_y;
                    //  }
                    if (VTDR6100_Fix255G_Flag == 0)
                    {
                        VTDR6100_GAMMA_Click(VTDR6100_GammaW_Red, null);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_Click(VTDR6100_GammaW_Green, null);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_Click(VTDR6100_GammaW_Blue, null);
                        Thread.Sleep(20);
                    }
                    else if (VTDR6100_Fix255G_Flag == 1)
                    {
                        VTDR6100_GAMMA_255fix_Click(0, r);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_255fix_Click(1, g);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_255fix_Click(2, b);
                        Thread.Sleep(20);
                    }
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }
        public void VTDR6100_dll_gamma_25P(Object myObject, EventArgs myEventArgs)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;

            frmHelp.Msr_CASDK();

            if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
                flag1 = 0;
            if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
                flag2 = 0;
            if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;
                if (index == 24)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "VTDR6100 25P gamma调试完毕!\r\n";
                }
                if (index < 24)
                {
                    switch (index)
                    {
                        case 0:
                            lum_off = (Convert.ToDouble(txtVT_255.Text) * Convert.ToDouble(textVT_255_off.Text)) / 100;
                            break;
                        case 1:
                            lum_off = (Convert.ToDouble(txtVT_223.Text) * Convert.ToDouble(textVT_223_off.Text)) / 100;
                            break;
                        case 2:
                            lum_off = (Convert.ToDouble(txtVT_191.Text) * Convert.ToDouble(textVT_191_off.Text)) / 100;
                            break;
                        case 3:
                            lum_off = (Convert.ToDouble(txtVT_159.Text) * Convert.ToDouble(textVT_159_off.Text)) / 100;
                            break;
                        case 4:
                            lum_off = (Convert.ToDouble(txtVT_127.Text) * Convert.ToDouble(textVT_127_off.Text)) / 100;
                            break;
                        case 5:
                            lum_off = (Convert.ToDouble(txtVT_111.Text) * Convert.ToDouble(textVT_111_off.Text)) / 100;
                            break;
                        case 6:
                            lum_off = (Convert.ToDouble(txtVT_95.Text) * Convert.ToDouble(textVT_95_off.Text)) / 100;
                            break;
                        case 7:
                            lum_off = (Convert.ToDouble(txtVT_79.Text) * Convert.ToDouble(textVT_79_off.Text)) / 100;
                            break;
                        case 8:
                            lum_off = (Convert.ToDouble(txtVT_71.Text) * Convert.ToDouble(textVT_71_off.Text)) / 100;
                            break;
                        case 9:
                            lum_off = (Convert.ToDouble(txtVT_63.Text) * Convert.ToDouble(textVT_63_off.Text)) / 100;
                            break;
                        case 10:
                            lum_off = (Convert.ToDouble(txtVT_55.Text) * Convert.ToDouble(textVT_55_off.Text)) / 100;
                            break;
                        case 11:
                            lum_off = (Convert.ToDouble(txtVT_47.Text) * Convert.ToDouble(textVT_47_off.Text)) / 100;
                            break;
                        case 12:
                            lum_off = (Convert.ToDouble(txtVT_39.Text) * Convert.ToDouble(textVT_39_off.Text)) / 100;
                            break;
                        case 13:
                            lum_off = (Convert.ToDouble(txtVT_35.Text) * Convert.ToDouble(textVT_35_off.Text)) / 100;
                            break;
                        case 14:
                            lum_off = (Convert.ToDouble(txtVT_31.Text) * Convert.ToDouble(textVT_31_off.Text)) / 100;
                            break;
                        case 15:
                            lum_off = (Convert.ToDouble(txtVT_27.Text) * Convert.ToDouble(textVT_27_off.Text)) / 100;
                            break;
                        case 16:
                            lum_off = (Convert.ToDouble(txtVT_23.Text) * Convert.ToDouble(textVT_23_off.Text)) / 100;
                            break;
                        case 17:
                            lum_off = (Convert.ToDouble(txtVT_19.Text) * Convert.ToDouble(textVT_19_off.Text)) / 100;
                            break;
                        case 18:
                            lum_off = (Convert.ToDouble(txtVT_15.Text) * Convert.ToDouble(textVT_15_off.Text)) / 100;
                            break;
                        case 19:
                            lum_off = (Convert.ToDouble(txtVT_11.Text) * Convert.ToDouble(textVT_11_off.Text)) / 100;
                            break;
                        case 20:
                            lum_off = (Convert.ToDouble(txtVT_7.Text) * Convert.ToDouble(textVT_7_off.Text)) / 100;
                            break;
                        case 21:
                            lum_off = (Convert.ToDouble(txtVT_5.Text) * Convert.ToDouble(textVT_5_off.Text)) / 100;
                            break;
                        case 22:
                            lum_off = (Convert.ToDouble(txtVT_3.Text) * Convert.ToDouble(textVT_3_off.Text)) / 100;
                            break;
                        case 23:
                            lum_off = (Convert.ToDouble(txtVT_1.Text) * Convert.ToDouble(textVT_1_off.Text)) / 100;
                            break;
                        default:
                            break;
                    }
                    if (VTDR6100_Fix255G_Flag == 1)
                    {
                        frmHelp.numPatternR.Value = Parttern_VTDR6100_1D[0];//固定255灰阶
                        frmHelp.numPatternG.Value = Parttern_VTDR6100_1D[0];
                        frmHelp.numPatternB.Value = Parttern_VTDR6100_1D[0];
                    }
                    else
                    {
                        frmHelp.numPatternR.Value = Parttern_VTDR6100_1D[index];//固定255灰阶
                        frmHelp.numPatternG.Value = Parttern_VTDR6100_1D[index];
                        frmHelp.numPatternB.Value = Parttern_VTDR6100_1D[index];
                    }
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                int flexible_gamma_count = 120;

                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R255.Value, VTDR6100_G255.Value, VTDR6100_B255.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R255.Value = r;
                                VTDR6100_G255.Value = g;
                                VTDR6100_B255.Value = b;

                                if (VTDR6100_G255.Value <= VTDR6100_G223.Value)
                                {
                                    VTDR6100_R223.Value = r - 20; VTDR6100_G223.Value = g - 20; VTDR6100_B223.Value = b - 20;
                                }
                                // auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692AX_R32.Value), gamma_g[index], Convert.ToUInt16(RM692AX_G32.Value), gamma_b[index], Convert.ToUInt16(RM692AX_B32.Value));
                            }
                            break;
                        case 1:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R223.Value, VTDR6100_G223.Value, VTDR6100_B223.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R223.Value = r;
                                VTDR6100_G223.Value = g;
                                VTDR6100_B223.Value = b;
                                if (VTDR6100_G223.Value <= VTDR6100_G191.Value)
                                {
                                    VTDR6100_R191.Value = r - 20; VTDR6100_G191.Value = g - 20; VTDR6100_B191.Value = b - 20;
                                }
                            }
                            break;
                        case 2:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R191.Value, VTDR6100_G191.Value, VTDR6100_B191.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R191.Value = r;
                                VTDR6100_G191.Value = g;
                                VTDR6100_B191.Value = b;
                                if (VTDR6100_G191.Value <= VTDR6100_G159.Value)
                                {
                                    VTDR6100_R159.Value = r - 20; VTDR6100_G159.Value = g - 20; VTDR6100_B159.Value = b - 20;
                                }
                            }
                            break;

                        case 3:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R159.Value, VTDR6100_G159.Value, VTDR6100_B159.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R159.Value = r;
                                VTDR6100_G159.Value = g;
                                VTDR6100_B159.Value = b;
                                if (VTDR6100_G159.Value <= VTDR6100_G127.Value)
                                {
                                    VTDR6100_R127.Value = r - 20; VTDR6100_G127.Value = g - 20; VTDR6100_B127.Value = b - 20;
                                }
                            }
                            break;
                        case 4:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R127.Value, VTDR6100_G127.Value, VTDR6100_B127.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R127.Value = r;
                                VTDR6100_G127.Value = g;
                                VTDR6100_B127.Value = b;
                                if (VTDR6100_G127.Value <= VTDR6100_G111.Value)
                                {
                                    VTDR6100_R111.Value = r - 20; VTDR6100_G111.Value = g - 20; VTDR6100_B111.Value = b - 20;
                                }
                            }
                            break;
                        case 5:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R111.Value, VTDR6100_G111.Value, VTDR6100_B111.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R111.Value = r;
                                VTDR6100_G111.Value = g;
                                VTDR6100_B111.Value = b;
                                if (VTDR6100_G111.Value <= VTDR6100_G95.Value)
                                {
                                    VTDR6100_R95.Value = r - 20; VTDR6100_G95.Value = g - 20; VTDR6100_B95.Value = b - 20;
                                }
                            }
                            break;
                        case 6:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R95.Value, VTDR6100_G95.Value, VTDR6100_B95.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R95.Value = r;
                                VTDR6100_G95.Value = g;
                                VTDR6100_B95.Value = b;
                                if (VTDR6100_G95.Value <= VTDR6100_G79.Value)
                                {
                                    VTDR6100_R79.Value = r - 20; VTDR6100_G79.Value = g - 20; VTDR6100_B79.Value = b - 20;
                                }
                            }
                            break;
                        case 7:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R79.Value, VTDR6100_G79.Value, VTDR6100_B79.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R79.Value = r;
                                VTDR6100_G79.Value = g;
                                VTDR6100_B79.Value = b;
                                if (VTDR6100_G79.Value <= VTDR6100_G71.Value)
                                {
                                    VTDR6100_R71.Value = r - 20; VTDR6100_G71.Value = g - 20; VTDR6100_B71.Value = b - 20;
                                }
                            }
                            break;
                        case 8:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R71.Value, VTDR6100_G71.Value, VTDR6100_B71.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R71.Value = r;
                                VTDR6100_G71.Value = g;
                                VTDR6100_B71.Value = b;
                                if (VTDR6100_G71.Value <= VTDR6100_G63.Value)
                                {
                                    VTDR6100_R63.Value = r - 20; VTDR6100_G63.Value = g - 20; VTDR6100_B63.Value = b - 20;
                                }
                            }
                            break;
                        case 9:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R63.Value, VTDR6100_G63.Value, VTDR6100_B63.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R63.Value = r;
                                VTDR6100_G63.Value = g;
                                VTDR6100_B63.Value = b;
                                if (VTDR6100_G63.Value <= VTDR6100_G55.Value)
                                {
                                    VTDR6100_R55.Value = r - 20; VTDR6100_G55.Value = g - 20; VTDR6100_B55.Value = b - 20;
                                }
                            }
                            break;
                        case 10:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R55.Value, VTDR6100_G55.Value, VTDR6100_B55.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R55.Value = r;
                                VTDR6100_G55.Value = g;
                                VTDR6100_B55.Value = b;
                                if (VTDR6100_G55.Value <= VTDR6100_G47.Value)
                                {
                                    VTDR6100_R47.Value = r - 20; VTDR6100_G47.Value = g - 20; VTDR6100_B47.Value = b - 20;
                                }
                            }
                            break;
                        case 11:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R47.Value, VTDR6100_G47.Value, VTDR6100_B47.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R47.Value = r;
                                VTDR6100_G47.Value = g;
                                VTDR6100_B47.Value = b;
                                if (VTDR6100_G47.Value <= VTDR6100_G39.Value)
                                {
                                    VTDR6100_R39.Value = r - 20; VTDR6100_G39.Value = g - 20; VTDR6100_B39.Value = b - 20;
                                }
                            }
                            break;
                        case 12:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R39.Value, VTDR6100_G39.Value, VTDR6100_B39.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R39.Value = r;
                                VTDR6100_G39.Value = g;
                                VTDR6100_B39.Value = b;
                                if (VTDR6100_G39.Value <= VTDR6100_G35.Value)
                                {
                                    VTDR6100_R35.Value = r - 20; VTDR6100_G35.Value = g - 20; VTDR6100_B35.Value = b - 20;
                                }
                            }
                            break;
                        case 13:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R35.Value, VTDR6100_G35.Value, VTDR6100_B35.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R35.Value = r;
                                VTDR6100_G35.Value = g;
                                VTDR6100_B35.Value = b;
                                if (VTDR6100_G35.Value <= VTDR6100_G31.Value)
                                {
                                    VTDR6100_R31.Value = r - 20; VTDR6100_G31.Value = g - 20; VTDR6100_B31.Value = b - 20;
                                }
                            }
                            break;
                        case 14:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R31.Value, VTDR6100_G31.Value, VTDR6100_B31.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R31.Value = r;
                                VTDR6100_G31.Value = g;
                                VTDR6100_B31.Value = b;
                                if (VTDR6100_G31.Value <= VTDR6100_G27.Value)
                                {
                                    VTDR6100_R27.Value = r - 20; VTDR6100_G27.Value = g - 20; VTDR6100_B27.Value = b - 20;
                                }
                            }
                            break;
                        case 15:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R27.Value, VTDR6100_G27.Value, VTDR6100_B27.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R27.Value = r;
                                VTDR6100_G27.Value = g;
                                VTDR6100_B27.Value = b;
                                if (VTDR6100_G27.Value <= VTDR6100_G23.Value)
                                {
                                    VTDR6100_R23.Value = r - 20; VTDR6100_G23.Value = g - 20; VTDR6100_B23.Value = b - 20;
                                }
                            }
                            break;
                        case 16:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R23.Value, VTDR6100_G23.Value, VTDR6100_B23.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R23.Value = r;
                                VTDR6100_G23.Value = g;
                                VTDR6100_B23.Value = b;
                                if (VTDR6100_G23.Value <= VTDR6100_G19.Value)
                                {
                                    VTDR6100_R19.Value = r - 15; VTDR6100_G19.Value = g - 15; VTDR6100_B19.Value = b - 15;
                                }
                            }
                            break;
                        case 17:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R19.Value, VTDR6100_G19.Value, VTDR6100_B19.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R19.Value = r;
                                VTDR6100_G19.Value = g;
                                VTDR6100_B19.Value = b;
                                if (VTDR6100_G19.Value <= VTDR6100_G15.Value)
                                {
                                    VTDR6100_R15.Value = r - 10; VTDR6100_G15.Value = g - 10; VTDR6100_B15.Value = b - 10;
                                }
                            }
                            break;
                        case 18:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R15.Value, VTDR6100_G15.Value, VTDR6100_B15.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R15.Value = r;
                                VTDR6100_G15.Value = g;
                                VTDR6100_B15.Value = b;
                                if (VTDR6100_G15.Value <= VTDR6100_G11.Value)
                                {
                                    VTDR6100_R11.Value = r - 5; VTDR6100_G11.Value = g - 5; VTDR6100_B11.Value = b - 5;
                                }
                            }
                            break;
                        case 19:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R11.Value, VTDR6100_G11.Value, VTDR6100_B11.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R11.Value = r;
                                VTDR6100_G11.Value = g;
                                VTDR6100_B11.Value = b;
                                if (VTDR6100_G11.Value <= VTDR6100_G7.Value)
                                {
                                    VTDR6100_R7.Value = r - 5; VTDR6100_G7.Value = g - 5; VTDR6100_B7.Value = b - 5;
                                }
                            }
                            break;
                        case 20:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R7.Value, VTDR6100_G7.Value, VTDR6100_B7.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R7.Value = r;
                                VTDR6100_G7.Value = g;
                                VTDR6100_B7.Value = b;
                                if (VTDR6100_G7.Value <= VTDR6100_G5.Value)
                                {
                                    VTDR6100_R5.Value = r - 5; VTDR6100_G5.Value = g - 5; VTDR6100_B5.Value = b - 5;
                                }
                            }
                            break;
                        case 21:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R5.Value, VTDR6100_G5.Value, VTDR6100_B5.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R5.Value = r;
                                VTDR6100_G5.Value = g;
                                VTDR6100_B5.Value = b;
                                if (VTDR6100_G5.Value <= VTDR6100_G3.Value)
                                {
                                    VTDR6100_R3.Value = r - 5; VTDR6100_G3.Value = g - 5; VTDR6100_B3.Value = b - 5;
                                }
                            }
                            break;
                        case 22:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], VTDR6100_R3.Value, VTDR6100_G3.Value, VTDR6100_B3.Value, Parttern_VTDR6100_1D[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R3.Value = r;
                                VTDR6100_G3.Value = g;
                                VTDR6100_B3.Value = b;
                                if (VTDR6100_G3.Value <= VTDR6100_G1.Value)
                                {
                                    VTDR6100_R1.Value = r - 5; VTDR6100_G1.Value = g - 5; VTDR6100_B1.Value = b - 5;
                                }
                            }
                            break;
                        case 23:
                            {
                                Gamma_adjust_timeout_flag = true;
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R1.Value = VTDR6100_R3.Value - 0x1;
                                VTDR6100_G1.Value = VTDR6100_G3.Value - 0x1;
                                VTDR6100_B1.Value = VTDR6100_B3.Value - 0x1;
                            }
                            break;
                        case 24:
                            {
                                Gamma_adjust_timeout_flag = true;
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                VTDR6100_R0.Value = 0;
                                VTDR6100_G0.Value = 0;
                                VTDR6100_B0.Value = 0;
                            }
                            break;
                        default:
                            break;
                    }
                    if (VTDR6100_Fix255G_Flag == 0)
                    {
                        VTDR6100_GAMMA_Click(VTDR6100_GammaW_Red, null);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_Click(VTDR6100_GammaW_Green, null);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_Click(VTDR6100_GammaW_Blue, null);
                        Thread.Sleep(20);
                    }
                    else if (VTDR6100_Fix255G_Flag == 1)
                    {
                        VTDR6100_GAMMA_255fix_Click(0, r);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_255fix_Click(1, g);
                        Thread.Sleep(20);
                        VTDR6100_GAMMA_255fix_Click(2, b);
                        Thread.Sleep(20);
                    }
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");

                }
            }
        }
    }
}
